package com.atguigu.eduucenter.controller;


import com.aiguigu.commonutils.R;
import com.atguigu.eduucenter.entity.UcenterMember;
import com.atguigu.eduucenter.entity.vo.RegisterUserVo;
import com.atguigu.eduucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-06
 */
@Api(description = "用户中心")
@RestController
@RequestMapping("/eduucenter/ucenterMember")
public class UcenterMemberController {

    @Resource
    private UcenterMemberService ucenterMemberService;

    /**
     * 登录
     * @param entity
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember entity){
        String token = ucenterMemberService.login(entity);
        return R.ok().data("token",token);
    }


    @ApiOperation("用户注册")
    @PostMapping("/registerUser")
    public R registerUser(@RequestBody RegisterUserVo entity){
        ucenterMemberService.register(entity);
        return R.ok().data(entity.getMobile());
    }

    @ApiOperation("手机号是否被注册")
    @GetMapping("/isExist/{mobile}")
    boolean isExist(
            @ApiParam(value = "用户名",required = true)
            @PathVariable String mobile){
        Boolean flag = ucenterMemberService.isExist(mobile);
        return flag;
    }

    @ApiOperation("根token获取用户信息")
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        UcenterMember ucenterMember = ucenterMemberService.getUserInfo(request);
        return R.ok().data(ucenterMember);
    }

    @ApiOperation("用户退出")
    @GetMapping("/logout")
    public R logout(HttpServletRequest request){
        ucenterMemberService.logout(request);
        return R.ok();
    }
}

